home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / SNIP0492.ARJ / FLN_FIX.C < prev    next >
C/C++ Source or Header  |  1991-09-14  |  4KB  |  143 lines

  1. /*
  2. **  FLN_FIX.C
  3. **
  4. **  Original Copyright 1988-1991 by Bob Stout as part of
  5. **  the MicroFirm Function Library (MFL)
  6. **
  7. **  This subset version is functionally identical to the
  8. **  version originally published by the author in Tech Specialist
  9. **  magazine and is hereby donated to the public domain.
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <dos.h>
  15. #include <io.h>
  16.  
  17. #define LAST_CHAR(string) (((char *)string)[strlen(string)-1])
  18.  
  19. typedef enum {ERROR = -1, FALSE, TRUE} LOGICAL;
  20.  
  21. char *unix2dos(char *path);
  22.  
  23. /****************************************************************/
  24. /*                                                              */
  25. /*  Function to `crunch' dot directories and check for          */
  26. /*  DOS-valid path strings. Drive specifiers in the path        */
  27. /*  ignored.                                                    */
  28. /*                                                              */
  29. /****************************************************************/
  30.  
  31. char *fln_fix(char *path)
  32. {
  33.       LOGICAL dir_flag = FALSE, root_flag = FALSE;
  34.       char *r, *p, *q, *s;
  35.  
  36.       if (path)
  37.             strupr(path);
  38.  
  39.       /* Ignore leading drive specs   */
  40.  
  41.       if (NULL == (r = strrchr(path, ':')))
  42.             r = path;
  43.       else  ++r;
  44.  
  45.       unix2dos(r);                      /* Convert Unix to DOS style    */
  46.  
  47.       while ('\\' == *r)                /* Ignore leading backslashes   */
  48.       {
  49.             if ('\\' == r[1])
  50.                   strcpy(r, &r[1]);
  51.             else
  52.             {
  53.                   root_flag = TRUE;
  54.                   ++r;
  55.             }
  56.       }
  57.  
  58.       p = r;                            /* Change "\\" to "\"           */
  59.       while (NULL != (p = strchr(p, '\\')))
  60.       {
  61.             if ('\\' ==  p[1])
  62.                   strcpy(p, &p[1]);
  63.             else  ++p;
  64.       }
  65.  
  66.       while ('.' == *r)                 /* Scrunch leading ".\"         */
  67.       {
  68.             if ('.' == r[1])
  69.             {
  70.                   /* Ignore leading ".."                                */
  71.  
  72.                   for (p = (r += 2); *p && (*p != '\\'); ++p)
  73.                         ;
  74.             }
  75.             else
  76.             {
  77.                   for (p = r + 1 ;*p && (*p != '\\'); ++p)
  78.                         ;
  79.             }
  80.             strcpy(r, p + ((*p) ? 1 : 0));
  81.       }
  82.  
  83.       while ('\\' == LAST_CHAR(path))   /* Strip trailing backslash     */
  84.       {
  85.             dir_flag = TRUE;
  86.             LAST_CHAR(path) = '\0';
  87.       }
  88.  
  89.       s = r;
  90.  
  91.       /* Look for "\." in path        */
  92.  
  93.       while (NULL != (p = strstr(s, "\\.")))
  94.       {
  95.             if ('.' == p[2])
  96.             {
  97.                   /* Execute this section if ".." found                 */
  98.  
  99.                   q = p - 1;
  100.                   while (q > r)           /* Backup one level           */
  101.                   {
  102.                         if (*q == '\\')
  103.                               break;
  104.                         --q;
  105.                   }
  106.                   if (q > r)
  107.                   {
  108.                         strcpy(q, p + 3);
  109.                         s = q;
  110.                   }
  111.                   else if ('.' != *q)
  112.                   {
  113.                         strcpy(q + ((*q == '\\') ? 1 : 0),
  114.                               p + 3 + ((*(p + 3)) ? 1 : 0));
  115.                         s = q;
  116.                   }
  117.                   else  s = ++p;
  118.  
  119.             }
  120.             else
  121.             {
  122.                   /* Execute this section if "." found                  */
  123.  
  124.                   q = p + 2;
  125.                   for ( ;*q && (*q != '\\'); ++q)
  126.                         ;
  127.                   strcpy (p, q);
  128.             }
  129.       }
  130.  
  131.       if (root_flag)  /* Embedded ".." could have bubbled up to root  */
  132.       {
  133.             for (p = r; *p && ('.' == *p || '\\' == *p); ++p)
  134.                   ;
  135.             if (r != p)
  136.                   strcpy(r, p);
  137.       }
  138.  
  139.       if (dir_flag)
  140.             strcat(path, "\\");
  141.       return path;
  142. }
  143.